package com.example.springbootshiro.controller;


import com.alibaba.fastjson.JSON;
import com.example.springbootshiro.entity.ShiroRole;
import com.example.springbootshiro.service.ShiroRoleService;
import org.apache.shiro.authz.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2022-01-11
 */
@Controller
@RequestMapping("/role")
public class ShiroRoleController {

    @Autowired
    private ShiroRoleService roleService;

    @RequestMapping("/test1")
    @RequiresGuest
    @ResponseBody
    public String test1(){
        return "test1";

    }

    @RequestMapping("/test2")
    @RequiresUser
    @ResponseBody
    public String test2(){
        return "test2";

    }

    @RequestMapping("/test3")
    @RequiresAuthentication
    @ResponseBody
    public String test3(){
        return "test3";

    }



    @RequestMapping("/test4")
    @RequiresRoles(value={"admin", "管理员"}, logical= Logical.OR)
    @ResponseBody
    public String test4(){
        return "test4";

    }

    @RequestMapping("/test5")
    @RequiresPermissions("user:delete")
    @ResponseBody
    public String test5(){
        return "test5";

    }

    @RequestMapping("/test6")
    @RequiresPermissions("user:add")
    @ResponseBody
    public String test6(){
        return "test6";

    }


    @RequestMapping("/getRoleByName")
    public String getRoleByName(String userName){
        List<ShiroRole> userRole = roleService.getUserRole(userName);
        return JSON.toJSONString(userRole);

    }

}
